package com.yzt.zhuju.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;

import com.yzt.zhuju.R;
import com.yzt.zhuju.widget.tuiview.TUITextView;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 个人资料模块的右上角按钮
 */
public class MenuSelectDialog extends Dialog implements View.OnClickListener {

    private TUITextView mBtn1;

    private TUITextView mBtn2;

    OnItemClickListener mListener;

    public MenuSelectDialog(Context context, int theme) {
        super(context, theme);
        this.initDialog();
    }

    public void initDialog() {
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        win.setGravity(8388688);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = -1;
        lp.height = -2;
        win.setAttributes(lp);
        this.setCanceledOnTouchOutside(false);
        this.show();
    }

    private void hideDialog() {

        this.cancel();

        this.dismiss();
    }

    public void setLogoutText(String content1, String content2) {
        mBtn1.setText(content1);
        mBtn2.setText(content2);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_select_menu);
        this.initView();
        this.initListener();
    }

    private void initView() {
        this.mBtn1 = this.findViewById(R.id.btn_logout);
        this.mBtn2 = this.findViewById(R.id.btn_cancel);
        findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initListener() {
        this.mBtn1.setOnClickListener(this);
        this.mBtn2.setOnClickListener(this);
        this.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == 4 && event.getAction() == 1) {
                MenuSelectDialog.this.hideDialog();
                MenuSelectDialog.this.mListener.onItemClick(0);
            }
            return false;
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.isOutOfBounds(this.getContext(), event)) {
            this.hideDialog();
            this.mListener.onItemClick(0);
        }
        return super.onTouchEvent(event);
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        View decorView = this.getWindow().getDecorView();
        return x < -slop || y < -slop || x > decorView.getWidth() + slop || y > decorView.getHeight() + slop;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_logout) {
            this.hideDialog();
            this.mListener.onItemClick(0);
        } else if (v.getId() == R.id.btn_cancel) {
            this.hideDialog();
            this.mListener.onItemClick(1);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {

        void onItemClick(int var1);
    }
}